Blow away corrupt database repos
authorAlex Crichton <alex@alexcrichton.com>
Mon, 1 Sep 2014 06:15:11 +0000 (23:15 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Tue, 2 Sep 2014 17:55:54 +0000 (10:55 -0700)
If a repository is corrupt, blow it away entirely and start from scratch instead
of returning an error.

src/cargo/sources/git/utils.rs

index efea0c18e99cfa9fca16719fde23d37943ba6ea2..5694e3dbd3c4e69f694f0e04ac1ba3ea76d6d1c0 100644 (file)
@@ -145,16 +145,18 @@ impl GitRemote {
     }
 
     pub fn checkout(&self, into: &Path) -> CargoResult<GitDatabase> {
-        let repo = if into.exists() {
-            let r = try!(git2::Repository::open(into));
-            try!(self.fetch_into(&r).chain_error(|| {
-                internal(format!("failed to fetch into {}", into.display()))
-            }));
-            r
-        } else {
-            try!(self.clone_into(into).chain_error(|| {
-                internal(format!("failed to clone into: {}", into.display()))
-            }))
+        let repo = match git2::Repository::open(into) {
+            Ok(repo) => {
+                try!(self.fetch_into(&repo).chain_error(|| {
+                    internal(format!("failed to fetch into {}", into.display()))
+                }));
+                repo
+            }
+            Err(..) => {
+                try!(self.clone_into(into).chain_error(|| {
+                    internal(format!("failed to clone into: {}", into.display()))
+                }))
+            }
         };
 
         Ok(GitDatabase { remote: self.clone(), path: into.clone(), repo: repo })
@@ -181,6 +183,9 @@ impl GitRemote {
 
     fn clone_into(&self, dst: &Path) -> CargoResult<git2::Repository> {
         let url = self.url.to_string();
+        if dst.exists() {
+            try!(rmdir_recursive(dst));
+        }
         try!(mkdir_recursive(dst, UserDir));
         let repo = try!(git2::build::RepoBuilder::new().bare(true)
                                                        .hardlinks(false)